<template>
	<view class="coupon-container">
		<!-- 自定义导航栏 -->
		<view class="custom-navbar" :style="{ paddingTop: statusBarHeight + 'px' }">
			<view class="navbar-content">
				<view class="back-btn" @click="back">
					<view class="back-arrow-icon"></view>
				</view>
				<view class="navbar-title">我的优惠券</view>
				<view class="navbar-right"></view>
			</view>
		</view>
		
		<!-- 分类切换 -->
		<view class="tabs-section">
			<view class="tab-item" 
			      :class="{ active: currentTab === 0 }" 
			      @click="switchTab(0)">
				全部
			</view>
			<view class="tab-item" 
			      :class="{ active: currentTab === 1 }" 
			      @click="switchTab(1)">
				未使用
			</view>
			<view class="tab-item" 
			      :class="{ active: currentTab === 2 }" 
			      @click="switchTab(2)">
				已使用
			</view>
			<view class="tab-item" 
			      :class="{ active: currentTab === 3 }" 
			      @click="switchTab(3)">
				已过期
			</view>
		</view>
		
		<!-- 优惠券列表 -->
		<view class="list-section">
			<view v-if="loading" class="loading-section">
				<u-loading-icon mode="spinner"></u-loading-icon>
				<text class="loading-text">加载中...</text>
			</view>
			
			<view v-else-if="couponList.length === 0" class="empty-section">
				<u-empty mode="data" text="暂无优惠券"></u-empty>
			</view>
			
			<view v-else class="coupon-list">
				<view class="coupon-item" 
				      v-for="(item, index) in couponList" 
				      :key="item.userCouponId || index"
				      :class="{ 'used': item.status === 1, 'expired': item.status === 2 }">
					<view class="coupon-left">
						<view class="coupon-amount">
							<text class="amount-symbol" v-if="item.type === 1 || item.type === 3">¥</text>
							<text class="amount-value">{{ getDiscountText(item) }}</text>
							<text class="amount-unit" v-if="item.type === 2">折</text>
						</view>
						<view class="coupon-type" v-if="item.type === 1">
							满{{ item.minAmount }}可用
						</view>
						<view class="coupon-type" v-else-if="item.type === 2">
							折扣券
						</view>
						<view class="coupon-type" v-else>
							直减券
						</view>
					</view>
					<view class="coupon-right">
						<view class="coupon-name">{{ item.name || '优惠券' }}</view>
						<view class="coupon-desc" v-if="item.description">
							{{ item.description }}
						</view>
						<view class="coupon-time">
							<text>有效期至：{{ formatDate(item.expireTime) }}</text>
						</view>
						<view class="coupon-status">
							<text class="status-text" v-if="item.status === 0">未使用</text>
							<text class="status-text used-text" v-else-if="item.status === 1">已使用</text>
							<text class="status-text expired-text" v-else>已过期</text>
						</view>
					</view>
				</view>
				<!-- 底部提示 -->
				<view class="list-end-tip" v-if="couponList.length > 0">
					<view class="end-line"></view>
					<text class="end-text">到底了哦</text>
					<view class="end-line"></view>
				</view>
			</view>
		</view>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
import $api from '../../api/user.js';

export default {
	data() {
		return {
			userId: null,
			currentTab: 0, // 0-全部，1-未使用，2-已使用，3-已过期
			couponList: [],
			loading: false,
			statusBarHeight: 0
		}
	},
	onLoad() {
		// 获取状态栏高度
		const systemInfo = uni.getSystemInfoSync();
		this.statusBarHeight = systemInfo.statusBarHeight || 0;
		
		this.loadUserInfo();
		this.loadCouponList();
	},
	onShow() {
		// 页面显示时刷新列表
		this.loadCouponList();
	},
	methods: {
		// 加载用户信息
		loadUserInfo() {
			const user = uni.getStorageSync('user');
			if (!user || !user.id) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				});
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				}, 1500);
				return;
			}
			
			this.userId = user.id;
		},
		
		// 切换分类
		switchTab(tab) {
			this.currentTab = tab;
			this.loadCouponList();
		},
		
		// 加载优惠券列表
		loadCouponList() {
			if (!this.userId) {
				return;
			}
			
			this.loading = true;
			
			const params = {
				userId: this.userId
			};
			
			// 如果选择了特定状态，添加状态参数
			if (this.currentTab === 1) {
				params.status = 0; // 未使用
			} else if (this.currentTab === 2) {
				params.status = 1; // 已使用
			} else if (this.currentTab === 3) {
				params.status = 2; // 已过期
			}
			
			$api.getCouponList(params).then(res => {
				this.loading = false;
				
				if (res.data && res.data.code === 200) {
					this.couponList = res.data.data || [];
				} else {
					this.$refs.uToast.show({
						title: res.data?.message || '获取优惠券列表失败',
						type: 'error',
					});
				}
			}).catch(err => {
				this.loading = false;
				console.error('获取优惠券列表失败', err);
				this.$refs.uToast.show({
					title: '获取优惠券列表失败，请重试',
					type: 'error',
				});
			});
		},
		
		// 获取优惠金额文本
		getDiscountText(item) {
			if (item.type === 2) {
				// 折扣券，显示折扣比例
				return (item.discountValue || 0).toString();
			} else {
				// 满减或直减，显示金额
				return (item.discountValue || 0).toFixed(0);
			}
		},
		
		// 格式化日期
		formatDate(dateStr) {
			if (!dateStr) return '';
			
			const date = new Date(dateStr);
			const year = date.getFullYear();
			const month = String(date.getMonth() + 1).padStart(2, '0');
			const day = String(date.getDate()).padStart(2, '0');
			
			return `${year}-${month}-${day}`;
		},
		
		back() {
			// 返回到"我的"界面（tabBar页面）
			uni.switchTab({
				url: '/pages/user/user'
			});
		}
	}
}
</script>

<style lang="scss" scoped>
page {
	background-color: #f5f5f5;
}

.coupon-container {
	min-height: 100vh;
	background-color: #f5f5f5;
	padding-top: 88rpx;
}

.custom-navbar {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	z-index: 991;
	background-color: #fff;
	border-bottom: 1rpx solid #e5e5e5;
	
	.navbar-content {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 88rpx;
		padding: 0 20rpx;
		
		.back-btn {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60rpx;
			height: 60rpx;
			padding: 0;
			position: relative;
			z-index: 1;
			
			.back-arrow-icon {
				width: 20rpx;
				height: 20rpx;
				border-left: 3rpx solid #606266;
				border-bottom: 3rpx solid #606266;
				transform: rotate(45deg);
			}
		}
		
		.navbar-title {
			flex: 1;
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
		}
		
		.navbar-right {
			width: 60rpx;
		}
	}
}

.tabs-section {
	display: flex;
	background-color: #fff;
	padding: 0;
	border-bottom: 1rpx solid #e5e5e5;
	
	.tab-item {
		flex: 1;
		text-align: center;
		font-size: 28rpx;
		color: #333;
		padding: 24rpx 0;
		position: relative;
		
		&.active {
			color: #333;
			font-weight: bold;
			
			&::after {
				content: '';
				position: absolute;
				bottom: 0;
				left: 50%;
				transform: translateX(-50%);
				width: 60rpx;
				height: 4rpx;
				background-color: #3c9cff;
				border-radius: 2rpx;
			}
		}
	}
}

.list-section {
	padding: 20rpx;
	background-color: #f5f5f5;
}

.loading-section {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 100rpx 0;
	
	.loading-text {
		margin-top: 20rpx;
		font-size: 26rpx;
		color: #999;
	}
}

.empty-section {
	padding: 100rpx 0;
}

.coupon-list {
	.coupon-item {
		display: flex;
		background-color: #fff;
		border-radius: 16rpx;
		margin-bottom: 20rpx;
		overflow: hidden;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
		position: relative;
		
		&::before {
			content: '';
			position: absolute;
			left: 200rpx;
			top: 0;
			bottom: 0;
			width: 2rpx;
			background: repeating-linear-gradient(
				to bottom,
				#f5f5f5 0,
				#f5f5f5 10rpx,
				transparent 10rpx,
				transparent 20rpx
			);
		}
		
		&.used, &.expired {
			opacity: 0.6;
			
			.coupon-left {
				background: linear-gradient(135deg, #ccc 0%, #999 100%);
			}
		}
		
		.coupon-left {
			width: 200rpx;
			background: linear-gradient(135deg, #ff6b6b 0%, #ff4757 100%);
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding: 30rpx 20rpx;
			color: #fff;
			
			.coupon-amount {
				display: flex;
				align-items: baseline;
				margin-bottom: 10rpx;
				
				.amount-symbol {
					font-size: 28rpx;
					font-weight: bold;
				}
				
				.amount-value {
					font-size: 56rpx;
					font-weight: bold;
					line-height: 1;
				}
				
				.amount-unit {
					font-size: 28rpx;
					margin-left: 4rpx;
				}
			}
			
			.coupon-type {
				font-size: 22rpx;
				opacity: 0.9;
			}
		}
		
		.coupon-right {
			flex: 1;
			padding: 30rpx 20rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			
			.coupon-name {
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
				margin-bottom: 10rpx;
			}
			
			.coupon-desc {
				font-size: 24rpx;
				color: #666;
				line-height: 1.5;
				margin-bottom: 10rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
			}
			
			.coupon-time {
				font-size: 22rpx;
				color: #999;
				margin-bottom: 10rpx;
			}
			
			.coupon-status {
				.status-text {
					display: inline-block;
					padding: 4rpx 16rpx;
					border-radius: 20rpx;
					font-size: 22rpx;
					background-color: #e8f5e9;
					color: #4caf50;
				}
				
				.used-text {
					background-color: #f5f5f5;
					color: #999;
				}
				
				.expired-text {
					background-color: #ffebee;
					color: #f44336;
				}
			}
		}
	}
	
	.list-end-tip {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 40rpx 0;
		
		.end-line {
			flex: 1;
			height: 1rpx;
			background-color: #e5e5e5;
		}
		
		.end-text {
			margin: 0 20rpx;
			font-size: 24rpx;
			color: #999;
		}
	}
}
</style>

